//
//  CheckXunJianParentViewController.m
//  Qihuo
//
//  Created by Vencoo on 13-12-30.
//  Copyright (c) 2013年 Vencoo. All rights reserved.
//

#import "CheckXunJianParentViewController.h"
#import "ParentCheckCell.h"
@interface CheckXunJianParentViewController ()
{
//    NSMutableArray *unitLabels;
}
@end

@implementation CheckXunJianParentViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self showMainView];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSURL *queryUrl = [NSURL URLWithString:_checkURL];
    ASIFormDataRequest *obtainFloor = [ASIFormDataRequest requestWithURL:queryUrl];
    [obtainFloor setPostValue:self.delegate.sendCheckTime forKey:@"checkTime"];
    [obtainFloor startAsynchronous];
    obtainFloor.delegate = self;
    [obtainFloor setDidFinishSelector:@selector(requestDone:)];
    [obtainFloor setDidFailSelector:@selector(requestFail)];
    
}
- (void)showMainView
{
    UIImage *lineImage = [UIImage imageNamed:@"黑边框"];
    UIImage *whiteimage = [UIImage imageNamed:@"底框-白.png"];
    UIImageView *MyView = [[UIImageView alloc]initWithFrame:CGRectMake((scrennWidth - whiteimage.size.width)/2, 90, whiteimage.size.width, whiteimage.size.height + 80)];
    MyView.image = whiteimage;
    [MyView setUserInteractionEnabled:YES];
    [self.view addSubview:MyView];
    
    UIImageView *lineView = [[UIImageView alloc]initWithImage:lineImage];
    lineView.frame = CGRectMake((whiteimage.size.width - lineImage.size.width)/2, 40 + (whiteimage.size.height - lineImage.size.height)/2, lineImage.size.width, lineImage.size.height + 40);
    lineView.userInteractionEnabled = YES;
    [MyView addSubview:lineView];
    
    UILabel *floorLb = [UILabel new];
    floorLb.text = @"楼层";
    floorLb.font = [UIFont systemFontOfSize:30];
    CGSize mysz = [floorLb.text sizeWithFont:floorLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    floorLb.frame = CGRectMake(lineView.frame.origin.x + 20, (lineView.frame.origin.y - mysz.height)/2, mysz.width, mysz.height);
    [MyView addSubview:floorLb];
    
    for (int i = 0; i < _checkNames.count; i ++)
    {
        UILabel *floorLbNew = [UILabel new];
        floorLbNew.text = [_checkNames objectAtIndex:i];
        floorLbNew.font = [UIFont systemFontOfSize:20];
        mysz = [[NSString stringWithFormat:@"啊啊啊啊"] sizeWithFont:floorLbNew.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
        floorLbNew.numberOfLines = 0;
        floorLbNew.textAlignment = 1;
        if (floorLbNew.text.length > 4)
        {
            floorLbNew.frame = CGRectMake((MyView.frame.size.width - 40 - floorLb.frame.size.width)/_checkNames.count*i+((lineView.frame.size.width - 40 - floorLb.frame.size.width)/_checkNames.count-mysz.width)/2 + floorLb.frame.origin.x + floorLb.frame.size.width, floorLb.frame.origin.y,mysz.width, mysz.height*2);
        }
        else
        {
            floorLbNew.frame = CGRectMake((MyView.frame.size.width - 40 - floorLb.frame.size.width)/_checkNames.count*i+((lineView.frame.size.width - 40 - floorLb.frame.size.width)/_checkNames.count-mysz.width)/2 + floorLb.frame.origin.x + floorLb.frame.size.width, floorLb.frame.origin.y + mysz.height/2,(MyView.frame.size.width - 40 - floorLb.frame.size.width)/_checkNames.count, mysz.height*(1 + mysz.width/(MyView.frame.size.width - 40 - floorLb.frame.size.width)/_checkNames.count));
        }
        [MyView addSubview:floorLbNew];
    }
    _mainTable = [[UITableView alloc]initWithFrame:CGRectMake(2, 2, lineImage.size.width-6, lineImage.size.height+20) style:UITableViewStylePlain];
    _mainTable.dataSource = self;
    _mainTable.delegate = self;
    [lineView addSubview:_mainTable];
    _mainTable.tag = 100;
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _floorNames.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = [NSString stringWithFormat:@"cell%d",indexPath.row];
    ParentCheckCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    int row = 0;
    if (!cell)
    {
        cell = [[ParentCheckCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        if (_floorCheckDatas.count > 0)
        {
            NSArray *checkDatas = [_floorCheckDatas objectAtIndex:indexPath.row];
            for (int i = 0; i < checkDatas.count; i ++)
            {
                NSDictionary *checkDic = [checkDatas objectAtIndex:i];
                NSString *checkResult = @"";
                if ([checkDic objectForKey:@"result"])
                {
                    checkResult = [NSString stringWithFormat:@"%@",[checkDic objectForKey:@"result"]];
                }
                else if ([checkDic objectForKey:@"isNormal"])
                {
                    checkResult = [NSString stringWithFormat:@"%@",[checkDic objectForKey:@"isNormal"]];
                }
                CGSize mySz;
//                if ([checkResult isEqualToString:@"0"])
//                {
//                    UIImage *rightImage = [UIImage imageNamed:@"正常"];
//                    mySz = rightImage.size;
//                    UIImageView *rightView = [[UIImageView alloc]initWithImage:rightImage];
//                    control = (UIControl*)rightView;
//                }
                
                UILabel *resultLb = [UILabel new];
                resultLb.text = checkResult;
                resultLb.font = [UIFont systemFontOfSize:20];
                mySz = [[NSString stringWithFormat:@"巴巴爸爸"] sizeWithFont:resultLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
                
//                int row = (resultLb.text.length-1)/4;
                if (resultLb.text.length <= 4)
                {
                    resultLb.frame = CGRectMake(cell.floorNameLb.frame.origin.x + cell.floorNameLb.frame.size.width + i * (900/(checkDatas.count + 1)),cell.floorNameLb.frame.origin.y, mySz.width, mySz.height);
                }
                else if(resultLb.text.length <= 8)
                {
                    resultLb.frame = CGRectMake(cell.floorNameLb.frame.origin.x + cell.floorNameLb.frame.size.width + i * (900/(checkDatas.count + 1)),cell.floorNameLb.frame.origin.y, mySz.width, mySz.height*2);
                    if (row < 1)
                    {
                        cell.frame = CGRectMake(0, 0, cell.frame.size.width ,resultLb.frame.size.height*1.5);
                        row = 1;
                    }
                }
                else if(resultLb.text.length <= 12)
                {
                    resultLb.frame = CGRectMake(cell.floorNameLb.frame.origin.x + cell.floorNameLb.frame.size.width + i * (900/(checkDatas.count + 1)),cell.floorNameLb.frame.origin.y, mySz.width, mySz.height*3);
                    if (row < 2)
                    {
                        cell.frame = CGRectMake(0, 0, cell.frame.size.width ,resultLb.frame.size.height*1.5);
                        row = 2;
                    }
                }
                else if (resultLb.text.length <= 16)
                {
                    resultLb.frame = CGRectMake(cell.floorNameLb.frame.origin.x + cell.floorNameLb.frame.size.width + i * (900/(checkDatas.count + 1)),cell.floorNameLb.frame.origin.y, mySz.width, mySz.height*4);
                    if (row < 3)
                    {
                        cell.frame = CGRectMake(0, 0, cell.frame.size.width ,resultLb.frame.size.height*2.5);
                        row = 3;
                    }
                }
                else if (resultLb.text.length <= 20)
                {
                    resultLb.frame = CGRectMake(cell.floorNameLb.frame.origin.x + cell.floorNameLb.frame.size.width + i * (900/(checkDatas.count + 1)),cell.floorNameLb.frame.origin.y, mySz.width, mySz.height*5);
                    if (row < 4)
                    {
                        cell.frame = CGRectMake(0, 0, cell.frame.size.width ,resultLb.frame.size.height*2.9);
                        row = 4;
                    }
                }
                resultLb.numberOfLines = 0;
                resultLb.textAlignment = 1;
                [cell addSubview:resultLb];
            }
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (_floorNames.count > 0) {
        cell.floorNameLb.text = [_floorNames objectAtIndex:indexPath.row];
    }
    if (_floorRemarks.count > 0) {
        cell.remarkLb.text = [_floorRemarks objectAtIndex:indexPath.row];
    }
    NSLog(@"row = %d",row);
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}
#pragma mark - request
- (void)requestDone:(ASIFormDataRequest *)request
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    NSString *response = [request responseString];
    NSDictionary *queryDic = [response JSONValue];
    NSLog(@"查询%@",queryDic);
    NSDictionary *queryDataArr = [queryDic objectForKey:@"data"];
    NSArray *floorData = [queryDataArr objectForKey:@"floorData"];
    
    if (floorData.count >0 &&[[floorData firstObject]valueForKey:@"floorName"])
    {
        _floorNames = [NSMutableArray array];
    }
    if (floorData.count >0 &&[[floorData firstObject]valueForKey:@"floorRemark"])
    {
        _floorRemarks = [NSMutableArray array];
    }
    if (floorData.count >0 &&[[floorData firstObject]valueForKey:@"checkData"])
    {
        _floorCheckDatas = [NSMutableArray array];
    }
    if (floorData.count >0 &&[[floorData firstObject]valueForKey:@"content"])
    {
        _floorContents = [NSMutableArray array];
    }
    if (floorData.count >0 &&[[floorData firstObject]valueForKey:@"result"])
    {
        _floorResults = [NSMutableArray array];
    }
    
    for (int i = 0; i < floorData.count; i ++)
    {
        NSDictionary *queryDic = [floorData objectAtIndex:i];
        if ([queryDic objectForKey:@"floorName"]) {
            [_floorNames addObject:[queryDic objectForKey:@"floorName"]];
        }
        if ([queryDic objectForKey:@"floorRemark"]) {
            [_floorRemarks addObject:[queryDic objectForKey:@"floorRemark"]];
        }
        if ([queryDic objectForKey:@"checkData"]) {
            [_floorCheckDatas addObject:[queryDic objectForKey:@"checkData"]];
        }
        if ([queryDic objectForKey:@"result"]) {
            [_floorResults addObject:[queryDic objectForKey:@"result"]];
        }
        if ([queryDic objectForKey:@"content"]) {
            [_floorContents addObject:[queryDic objectForKey:@"content"]];
        }
    }
    [self.mainTable reloadData];
}
- (void)requestFail
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    UIAlertView *failAlert = [[UIAlertView alloc]initWithTitle:@"警告" message:@"请求数据失败！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [failAlert show];
}
#pragma mark - end
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
